import 'package:family_expense/features/account_user/models/account_group.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

/// The service communicates with backend for [AccountGroupUser] usage.
class AccountGroupUserService {
  final String _url = '/account-group-user';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves current account group users from backend service.
  Future<List<AccountGroup>> queryAvailableAccountGroups() async {
    // Gets the response of the http get request.
    final response =
        await _httpRequestService.get('$_url/query-available-account-groups');

    // return List<Map<String, dynamic>>.from(response.payload);

    // Parses the data from [Map<String, dynamic>] to [AccountGroup].
    return (response.payload as List<dynamic>)
        .map((p) => AccountGroup.fromJson(p))
        .toList();
  }

  /// Deletes a new invitation request by id.
  Future<void> remove(User user) async {
    await _httpRequestService.post('$_url/remove', body: {
      'userId': user.id,
      'username': user.username,
    });
  }

  /// Quits this account group.
  Future<void> quit(String accountGroupId) async {
    await _httpRequestService
        .post('$_url/quit', body: {'accountGroupId': accountGroupId});
  }
}
